<?php
/**
 * +--------------------------------------------------------+
 * @Created by Phpstorm.
 * @Category MyStockRepository.php
 * @Depiction
 * @Author hwy <429208948@qq.com>
 * @Date 2022/4/24  16:52
 * +--------------------------------------------------------+
 */

namespace App\Repositories;

use App\Models\MyStock;
use Carbon\Carbon;
use Illuminate\Support\Facades\Cache;

class MyStockRepository extends BaseRepository
{
    protected $myStock;

    public function __construct(MyStock $myStock)
    {
        $this->myStock = $myStock;
    }

    /**
     * @param $params
     * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
     * @Depiction
     * @Author hwy <429208948@qq.com>
     * @Date 2022/6/30  16:40
     */
    public function getList($params)
    {
        $query = $this->myStock->query();

        $perPage = $this->perPage($params);
        [$sort, $order] = $this->paramsSet($params);

        if (!empty($params['title'])) {
            $query = $query->where('content', 'like', '%' . $params['title'] . '%')
                ->orWhere('title', 'like', '%' . $params['title'] . '%');
        }

        return $list = $query->orderBy($sort, $order)->paginate($perPage);
    }

    public function addData($params)
    {

    }

    /**
     * @return array
     * @Depiction
     * @Author hwy <429208948@qq.com>
     * @Date 2022/6/30  16:28
     */
    public function getAllByInit(): array
    {
        $stock_data = $this->myStock->query()
            ->select('code', 'name')
            ->where('is_init', 1)
            ->get()
            ->pluck('name', 'code');

        return $stock_data->toArray() ?? [];
    }

    /**
     * @param $stock_list
     * @param string $url
     * @return array
     * @Depiction 查询实时数据情况 curl 外部数据
     * @Author hwy <429208948@qq.com>
     * @Date 2022/6/30  16:40
     */
    public function curlStockRealTime($stock_url = ''): array
    {
        $stock_data = curl($stock_url);
        $stock_data = iconv("gb2312", "utf-8//IGNORE", $stock_data);
        $stock_data = explode(';', $stock_data);
        array_pop($stock_data);

        $data_list = $stock_data;

        $return_data = [];
        foreach ($data_list as $key => $val) {
            if (!empty($val)) {
                $return_data[$key] = explode('~', $val);
            }
        }

        return $return_data;
    }

    /**
     * @param int $type
     * @return mixed
     * @Depiction 获取数据
     * @Author hwy <429208948@qq.com>
     * @Date 2022/7/4  17:28
     */
    public function getStocks($type){
        $cache_name = 'stock_list';
        $cache_ttl  = Carbon::now()->addHours(2);
        if ($type == 1) {
            Cache::pull($cache_name);
        }
        $data = Cache::remember($cache_name, $cache_ttl, function () {
            return $this->myStock->orderBy('warm', 'asc')
                ->orderBy('attention', 'asc')
                ->orderBy('id', 'desc')
                ->get()
                ->pluck('name', 'code');
        });

        return $data;
    }

}
